package rd.android.hardcore 
{
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Screen;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.system.Capabilities;
	import flash.utils.setTimeout;
	import rd.android.hardcore.engine.android.Utils;
	import rd.android.hardcore.event.GlobalEvent;
	import rd.android.hardcore.params.GlobalParams;
	import rd.android.hardcore.ui.SplashScreen;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class Launcher extends Sprite
	{
		private var main:Main;
		private var splash:SplashScreen;
		
		public function Launcher() 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			trace( Screen.mainScreen.bounds.width);
			trace( Screen.mainScreen.bounds.height);
			
			splash = new SplashScreen();
			splash.alpha = 0;
			addChild(splash);
			
			Tweener.addTween(splash, { alpha:1, time:2} );
			
			main = new Main();
			main.alpha = 0;
			main.addEventListener(GlobalEvent.MAIN_DISPLAYED, allDataSet);
			
			
			handleResize();
			
			stage.addEventListener(Event.RESIZE, handleResize);
		}
		
		private function allDataSet(e:GlobalEvent):void 
		{
			setTimeout(disposeSplashScreen,2000);
		}
		
		private function disposeSplashScreen():void 
		{
			addChild(main);
			Tweener.addTween(main, { alpha:1, time:.5, delay:.3} );
			Tweener.addTween(splash, { alpha:0, time:.5, onComplete:removeChild, onCompleteParams:[splash]} );
		}
		
		private function handleResize(e:Event = null ):void 
		{
			if (splash) {
				splash.x = (stage.stageWidth - splash.width) >> 1;
				splash.y = (stage.stageHeight - splash.height) >> 1;
			}
			if (main) {
				main.handleResize(e);
			}
			
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x333333, 0x939396];
			var alphas:Array = [1, 1];
			var ratios:Array = [0, 255];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(GlobalParams.WIDTH, GlobalParams.HEIGHT, Math.PI * .5);
			var spreadMethod:String = SpreadMethod.PAD;
			graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  
			graphics.drawRect(0,0,GlobalParams.WIDTH, GlobalParams.HEIGHT);
			graphics.endFill();
			
			
		}
		
	}

}
